/*
 * Copyright (c) 2009 Nathaniel Houghton <nathan@brainwerk.org>
 *                    Robert Kirchgessner <rkirchge@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for
 * any purpose with or without fee is hereby granted, provided that
 * the above copyright notice and this permission notice appear in all
 * copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
 * OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 */

#ifndef ELM_H
#define ELM_H

#include <stdint.h>
#include <stdio.h>

#include "elm_prot.h" /*Contains the different protocols supported by the ELM chip.*/
 
#define DEV_ELM 1 /*The ELM is usart device 1*/

#define elm_pwrwait(void) elm_flush('>')

typedef struct {
	uint32_t 	caps[3]; /*Which pids are supported. There are 3 sets of PIDs.*/
	uint8_t 	prot; /*The current protocol set*/
	uint8_t		eid; /*For canbus*/
	uint8_t		initd; /*Has the interface been initialized*/
} elm_dev;

typedef struct { 
	uint16_t code; /*Current DRC code*/
} drc;

extern uint8_t get_pid_error;
extern elm_dev myelm;

#define ELM_INIT_TRIES 2

uint8_t 	elm_init( elm_dev *dev, uint8_t prot ); /*Initialize the ELM device. Returns 1 if success.*/
uint8_t		elm_init_retry(elm_dev *dev, uint8_t prot ); /*Do init with retries. For Nathans ancient car!*/
uint8_t 	elm_getpid( uint8_t pid, uint8_t *buf, uint16_t len ); /*Gets the requested PID. Current version does not verify that the PID is supported (up to host software*/
void		elm_reset();
void 		elm_setecho( uint8_t val ); /*Set echo on/off*/

/*Generic functions*/
uint8_t elm_gets(char *buf, uint16_t len); /*Reads a string*/
uint8_t elm_readdata( uint8_t *buf, uint16_t len ); /*Read a hex string into a byte array*/
void 	elm_flush( uint8_t ch ); /*Flush till character*/

/*Inline functions*/
void elm_set_protocol( uint8_t protocol ); /*Set the protocol with auto search*/

#endif
